<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class kst_shu extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('kst_shu_model', 'shu');
		$this->load->model('kst_dt_shu_model', 'Detail');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup SHU";
		viewByTemplate('kst_shu/index', $data, 'grid');
	}
	 
	function getdata()
	{
		$this->secure_session();
		echo $this->shu->GetJSON();
	}
function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->Detail->GetJSON(post('shu_id'));
	}

	function create()
	{
		// Display Form Create
		$data['title'] = "SHU Baru";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";	
		viewByTemplate('kst_shu/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah SHU";
		$data['data'] = $this->shu->GetSingleView(get('shu_id'));
		
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('kst_shu/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View SHU";
		$data['data'] = $this->shu->GetSingleView(get('shu_id'));
		
		$data['mode'] = "view";

		viewByTemplate('kst_shu/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("shu_remark") == '') {
			$error->Add("shu_remark", "Remark", $tpl_empty);
		}else if (post("shu_awal") == '') {
			$error->Add("shu_awal", "Tanggal Mulai", $tpl_empty);
		}else if (post("shu_akhir") == '') {
			$error->Add("shu_akhir", "Tanggal Akhir", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				
				if (post("mode") == "create")
				{
					$query = $this->db->query("SELECT shu_id FROM (SELECT * FROM kst_shu ORDER BY shu_id DESC) WHERE rownum <= 1");
					
					if($query->num_rows() > 0){
						$getquery = $query->row_array();
						$rowcount = $getquery['shu_id'];
						$autonum = $rowcount + 1;
					} else {
						$autonum = 1;
					}
				}
				else if (post("mode") == "edit")
				{
					$query = $this->db->query("SELECT shu_id FROM (SELECT * FROM kst_shu where shu_id = '".$this->input->post('shu_id')."' ORDER BY shu_id DESC) WHERE rownum <= 1");
					
					if($query->num_rows() > 0){
						$getquery = $query->row_array();
						$rowcount = $getquery['shu_id'];
						$autonum = $rowcount;
					} else {
						$autonum = 1;
					}
				}

				$shu_id_x = $autonum;

				$tgl_old = str_replace(" ", "", $this->input->post('shu_awal'));
				$tahun_new = substr($tgl_old, 6, 4);
			    $bulan_new = substr($tgl_old, 3, 2);
			    $hari_new   = substr($tgl_old, 0, 2);
			    $tgl_new = $tahun_new."-".$bulan_new."-".$hari_new;

			    $tgl_old1 = str_replace(" ", "", $this->input->post('shu_akhir'));
				$tahun_new1 = substr($tgl_old1, 6, 4);
			    $bulan_new1 = substr($tgl_old1, 3, 2);
			    $hari_new1   = substr($tgl_old1, 0, 2);
			    $tgl_new1 = $tahun_new1."-".$bulan_new1."-".$hari_new1;

				$data = array(
								'shu_id' => $autonum,
								'shu_remark' => $this->input->post('shu_remark'),
								'shu_awal' => $tgl_new,
								'shu_akhir' => $tgl_new1,
								);

						// $data['return_toko_id']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['return_toko_id'])));
//						$data['return_toko_no']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['return_toko_no'])));

				if (post("mode") == "create")
				{
					$this->shu->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->shu->Update($data, array('shu_id'	=> $data['shu_id']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}


			
			// ----- >> Detail Sample 2
				$dtl_sample2	= $this->data['dtl_sample2'];
				$total_deleted	= $dtl_sample2['num_row']['deleted'];
				$total_inserted	= $dtl_sample2['num_row']['inserted'];
				$total_updated	= $dtl_sample2['num_row']['updated'];
				

				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_sample2['deleted'][$i];					
					$this->Detail->DeleteSingle($row['shu_dt_id']);
				}
				
				
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					
					// Auto number kode detail return
					
					
					$query = $this->db->query("SELECT shu_dt_id FROM (SELECT * FROM kst_detail_shu ORDER BY shu_dt_id DESC) WHERE rownum <= 1");
					
					if($query->num_rows() > 0){
						$getquery = $query->row_array();
						$rowcount = $getquery['shu_dt_id'];
						$autonum = $rowcount + 1;
					} else {
						$autonum = 1;
					}

					$row = $dtl_sample2['inserted'][$i];
					$row['shu_dt_id'] = $autonum;
					$row['shu_id'] = $shu_id_x;
					$row['shu_dt_class'] = $row['shu_dt_class'];
					$row['shu_dt_amount'] = $row['shu_dt_amount'];
					$this->Detail->Add($row);
				}
				
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_sample2['updated'][$i];					
					$shu_dt_id	= $row['shu_dt_id'];
					unset($row['shu_dt_id']);					
					//$row['jumlah'] = (float)$row['jumlah'];					
					$this->Detail->Update($row, array('shu_dt_id'	=> $shu_dt_id));
				}
			
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}




	function ajax_delete()
	{
		$ls_result	= "";
		$shu_id	= get('shu_id');
		
		if ($shu_id != "")
		{
			// Delete Single
			$arr_rule	= array('shu_id' => $shu_id);
			$ls_result = $this->shu->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('shu_id' => $row['shu_id']);
				$ls_result .= $this->shu->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
	
}
?>